<?php
/**
 * 
 * Jquery controller
 * 
 * @category Sfer
 * 
 * @package Sfer_Controller
 * 
 * @author Richard Thomas <richard@phpjack.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: Auth.php 3850 2009-06-24 20:18:27Z pmjones $
 * 
 */
class Sfer_Controller_Jquery extends Solar_Controller_Page 
{

    /**
     * 
     * Default configuration values.
     * 
     * @config string adapter The adapter class, for example 'Solar_Auth_Adapter_File'.
     * 
     * @var array
     * 
     */
    protected $_Sfer_Controller_Jquery = array(
        'formats'   => array('json', 'xml', 'html'),
    );

    /**
     * 
     * HTML, Json or XML
     * 
     * @var string/array
     * 
     */
    protected $_data;

    /**
     * 
     * Is this a get or post request
     * 
     * @const string
     * 
     */
    protected $_type = 'get';

    /**
     * 
     * What handler are we using.. used to convert to proper return format
     * 
     * @const string
     * 
     */
    protected $_handler = false;



    /**
     * 
     * Does the basic setup.
     * 
     *  What type of request are we dealing with and what handler do we have
     * 
     * @return boolean
     *  
     */
    protected function _setup() 
    {
        // We only want to handle the proper requests
        if (! $this->_request->isXhr()) {
            return false;
        }
        if ($this->_request->isPost()) {
            $this->_type = 'post';
        }
        if ($this->_request->{$this->_type}('handler')) {
            $this->_handler = $this->_request->{$this->_type}('handler');
        }
        return true;
    }


    /**
     * 
     * Allows the user to set the current data pattern
     *   
     */ 
    protected function _setData($data)
    {
        $this->_data = $data;
    }

    /**
     * 
     * Get the request params
     *   
     * Pull the proper params based on request type from the Ajax request
     * 
     * @return array
     *  
     */ 
    protected function _getParams()
    {
        return $this->_request->{$this->_type}('params');
    }

    /**
     * 
     * Convert _data based on handler if required then output
     *   
     */ 
    protected function _preRender()
    {
        if (in_array($this->_handler, $this->_config['formats'])) {
            $this->_data = $this->_handleFormats($this->_data);
        }
        echo $this->_data;
        exit();
    }

    /**
     * 
     * handles converting the data as needed and setting any headers
     *   
     * @return string
     *  
     */ 
    protected function _handleFormats($data)
    {
        switch($this->_handler) {
            case 'json':
                return '(' . json_encode($data) . ')';
            break;
            case 'xml':
                header('Content-Type: text/xml');
            default:
                return $data;
            break;
        }
    }

    /**
     * 
     * Used to build json requests
     *   
     * Formats method/args into a json variable that we can parse in javascript and act on
     * 
     */ 
    public function __call($method, $args)
    {
        $this->_handler = 'json';
        if(sizeof($args) == 1 && !is_array($args[0])) {
          $args = $args[0];
        } 
        $rep = array();
        $rep['m'] = $method;
        if($args) {
            $rep['a'] = $args;
        }
        $this->_data[] = $rep;
    }


}